﻿/**
 * © Soft Sarjon 2012, All rights reserverd.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Erebos.ScreenFiles
{
    public delegate void OnClick(object sender);

    public class Button
    {
        private string _name;
        private Texture2D _texture;

        private Vector2 _position;
        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        private Vector2 _padding;
        private SpriteFont _font;
        private MouseState _oldMouseState = Mouse.GetState();
        public event OnClick OnClick;

        public Button(string name, string fontName, Texture2D texture, Vector2 position, Vector2 padding)
        {
            _name = name;
            _texture = texture;
            _position = position;
            _padding = padding;
            _font = MainGame.Fonts[fontName];
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_texture, _position, Color.White);
            spriteBatch.DrawString(_font, _name, _position + _padding, Color.DarkRed);
        }

        public void Update()
        {
            if (_oldMouseState.LeftButton == ButtonState.Pressed && Mouse.GetState().LeftButton == ButtonState.Released)
            {
                Rectangle r = new Rectangle((int)_position.X, (int)_position.Y, _texture.Width, _texture.Height);
                if (r.Contains(Mouse.GetState().X, Mouse.GetState().Y))
                {
                    if (OnClick != null)
                        OnClick(this);
                }
            }
            _oldMouseState = Mouse.GetState();
        }
    }
}
